//导入高德地图APP
var amapFile = require('../../libs/amap-wx.js');
//获取应用实例
const app = getApp()

Page({
  data: {
    address:'',
    weather:'',
    temperature:'',
    humidity:'',
    windpower:'',
    winddirection:'',
    news:'',
    localNews:''
  },


  onLoad: function () {
    var that = this;
    var result = null;
    var myAmapFun = new amapFile.AMapWX({key:'a95d94be021a9c247ff05d92527e3807'});
    //通过高德地图获取天气的地理信息
    myAmapFun.getWeather({
      success: function(data){
        //成功回调
        that.setData({
          address: data.liveData.city,
          humidity: data.liveData.humidity,
          temperature: data.liveData.temperature,
          weather: data.liveData.weather,
          winddirection: data.liveData.winddirection,
          windpower: data.liveData.windpower
        })
      },
      fail: function(info){
        //失败回调
        console.log(info)
      }
    })

    //获取头条新闻
    // wx.request({
    //   url: 'http://v.juhe.cn/toutiao/index',
    //   data:{
    //     "key":"6a2a852334cda8bace26773c216e5b6b",
    //     "type":"keji"
    //   },
    //   success(res){
    //     result = res.data.result;
    //     var test = JSON.stringify(result);
    //     console.log(test);
    //     that.setData({
    //       news:result.data,
    //     })
    //   }
    // })

    //获取云数据库头条新闻
    wx.request({
      url: 'http://47.107.36.170:3001/api/news_list/0',
      data: {
        // "type": "5"
      },
      success(res) {
        result = res.data;
        var test = JSON.stringify(result);
        console.log("local: ",test);
        that.setData({
          localNews: result.data,
        })
      }
    })

  },

  //跳转到新闻详情
  toNews:function(e){
    let url = JSON.stringify(e.currentTarget.dataset.url);
    console.log("url: ",url);
    wx.navigateTo({
      url: '../out/out?url=' + url,
    });
  }


})
